Skip to contentMethod: toAxiom(NamedResource, OWLDataPropertyAssertionAxiom, boolean)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.model.*;
18: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
19: import org.semanticweb.owlapi.model.*;
20:
21: /**
22: * Adapts OWLAPI axioms to JOPA (OntoDriver) axioms and vice versa.
23: */
24: public class AxiomAdapter {
25:
26: private final OWLDataFactory dataFactory;
27: private final String language;
28:
29: public AxiomAdapter(OWLDataFactory dataFactory, String language) {
30: this.dataFactory = dataFactory;
31: this.language = language;
32: }
33:
34: public OWLAxiom toOwlClassAssertionAxiom(Axiom<?> axiom) {
35: final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(axiom.getValue().stringValue()));
36: return dataFactory.getOWLClassAssertionAxiom(owlClass, toOWLIndividual(axiom.getSubject()));
37: }
38:
39: public OWLAxiom toOwlObjectPropertyAssertionAxiom(Axiom<?> axiom) {
40: final OWLObjectProperty objectProperty = dataFactory
41: .getOWLObjectProperty(IRI.create(axiom.getAssertion().getIdentifier()));
42: final OWLNamedIndividual objectValue = dataFactory.getOWLNamedIndividual(
43: IRI.create(axiom.getValue().stringValue()));
44: return dataFactory.getOWLObjectPropertyAssertionAxiom(objectProperty, toOWLIndividual(axiom.getSubject()),
45: objectValue);
46: }
47:
48: public OWLAxiom toOwlDataPropertyAssertionAxiom(Axiom<?> axiom) {
49: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(
50: IRI.create(axiom.getAssertion().getIdentifier()));
51: final OWLLiteral dataValue = OwlapiUtils.createOWLLiteralFromValue(axiom.getValue().getValue(),
52: dataFactory, language);
53: return dataFactory
54: .getOWLDataPropertyAssertionAxiom(dataProperty, toOWLIndividual(axiom.getSubject()), dataValue);
55: }
56:
57: public OWLAxiom toOwlAnnotationPropertyAssertionAxiom(Axiom<?> axiom) {
58: final OWLAnnotationProperty annotationProperty = dataFactory.getOWLAnnotationProperty(IRI.create(
59: axiom.getAssertion().getIdentifier()));
60: final Object value = axiom.getValue().getValue();
61: final OWLAnnotationValue annotationValue;
62: if (OwlapiUtils.isIndividualIri(value)) {
63: annotationValue = IRI.create(value.toString());
64: } else {
65: annotationValue = OwlapiUtils.createOWLLiteralFromValue(
66: axiom.getValue().getValue(), dataFactory, language);
67: }
68: return dataFactory
69: .getOWLAnnotationAssertionAxiom(annotationProperty, toOWLIndividual(axiom.getSubject()).getIRI(),
70: annotationValue);
71: }
72:
73: public OWLNamedIndividual toOWLIndividual(NamedResource subject) {
74: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
75: }
76:
77: public Axiom<?> toAxiom(NamedResource subject, OWLDataPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
78: final Assertion assertion = Assertion
79: .createDataPropertyAssertion(assertionAxiom.getProperty().asOWLDataProperty().getIRI().toURI(),
80: isInferred);
81: return createAxiom(subject, assertion, OwlapiUtils.owlLiteralToValue(assertionAxiom.getObject()));
82: }
83:
84: public <V> Axiom<V> createAxiom(NamedResource subject, Assertion assertion, V value) {
85: return new AxiomImpl<>(subject, assertion, new Value<>(value));
86: }
87:
88: public Axiom<?> toAxiom(NamedResource subject, OWLObjectPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
89: final Assertion assertion = Assertion
90: .createObjectPropertyAssertion(assertionAxiom.getProperty().asOWLObjectProperty().getIRI().toURI(),
91: isInferred);
92: final IRI target = assertionAxiom.getObject().asOWLNamedIndividual().getIRI();
93: return createAxiom(subject, assertion, NamedResource.create(target.toURI()));
94: }
95:
96: public Axiom<?> toAxiom(NamedResource subject, OWLAnnotationAssertionAxiom assertionAxiom, boolean isInferred) {
97: final Assertion assertion = Assertion
98: .createAnnotationPropertyAssertion(
99: assertionAxiom.getProperty().asOWLAnnotationProperty().getIRI().toURI(),
100: isInferred);
101: if (assertionAxiom.getValue().asIRI().isPresent()) {
102: return createAxiom(subject, assertion, assertionAxiom.getValue().asIRI().get().toURI());
103: } else {
104: return createAxiom(subject, assertion, OwlapiUtils.owlLiteralToValue(
105: assertionAxiom.getValue().asLiteral().get()));
106: }
107: }
108: }